package com.boilerCloud.timer;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boilerCloud.dto.device.DeviceChannels;
import com.boilerCloud.dto.device.DeviceData;
import com.boilerCloud.dto.device.DeviceInfo;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.service.device.IDeviceService;
import com.boilerCloud.util.RedisPoolsUtil;
import com.boilerCloud.util.StringUtil;
import com.fbox.Main;

@Configuration          //证明这个类是一个配置文件  
@EnableScheduling       //打开quartz定时器总开关  
public class Timer {  

	@Resource
	private IDeviceService deviceService;

	private long currtime = 0L;

	@Scheduled(cron = "0 0/5 * * * *")  
	public void timer(){  
	    //获取当前时间  
	    LocalDateTime localDateTime =LocalDateTime.now();  
	    System.out.println("当前时间为:" + localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));  
	    List<String> boxIds = deviceService.qryBoxIds();
	    for (int i = 0; i < boxIds.size(); i++) {
	    	JSONObject json = Main.getBoxInfo(boxIds.get(i));
			if(json != null){			
				String box_id =json.getString("uid");
				if(box_id == null){
					box_id = json.getJSONObject("box").getString("uid");
				}
				int state = json.getJSONObject("box").getJSONObject("commServer").getIntValue("state");
				deviceService.updateState(state,box_id);
			}
		}
	}  
	
	
	@Scheduled(cron = "0 0/5 * * * *")  
	public void timer1(){  
	    //获取当前时间  
		try {
	    LocalDateTime localDateTime =LocalDateTime.now();  
	    System.out.println("当前时间为:" + localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));  
	    List<DeviceInfo> boxs = deviceService.qrydeviceBoxIds();
		
	    DeviceInfo box = null;
	    long time = System.currentTimeMillis();
	    for (int i = 0; i < boxs.size(); i++) {
	    	box = boxs.get(i);
	    	JSONArray array = Main.getAlarmRecord(box.getBoxId(),box.getQueryTime(),time);
	    	deviceService.addWarnInfo(array,box.getId());
			
	    	deviceService.updateQueryTime(time, box.getId());
		}
	    
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}  

	/**
	 * 获取设备数据
	 */
	@Scheduled( cron = "0 0/10 * * * *" )
	public void timer2() {

		//获取当前时间  
		try {
			LocalDateTime localDateTime = LocalDateTime.now();
			System.out.println("当前时间为:" + localDateTime.format(DateTimeFormatter.ofPattern(
					"yyyy-MM-dd HH:mm:ss")));
			List<DeviceInfo> boxs = deviceService.qrydeviceBoxIds();

			DeviceInfo box = null;
			JSONObject object = null;
			JSONArray rows = null;
			List<Long> listIds = null;
			long last_time = LocalDateTime.now().plusDays(-7).withHour(0).withMinute(0).withSecond(0)
					.withNano(0)
					.toInstant(ZoneOffset.of("+8")).toEpochMilli();
			if (!StringUtil.isEmpty(RedisPoolsUtil.get("last_time"))) {
				last_time = Long.parseLong(RedisPoolsUtil.get("last_time"));
			}
			currtime = System.currentTimeMillis();
			DeviceData data = null;
			for (int i = 0; i < boxs.size(); i++) {
				box = boxs.get(i);
				List<DeviceChannels> array = deviceService.qryChannel(box.getId(), new ReqResult());
				for (int j = 0; j < array.size(); j++) {
					listIds = Arrays.asList(array.get(j).getChildUid()
							.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors
									.toList());
					//object = Main.hdata(listIds.toArray(new Long[0]));
					object = Main.hdata(listIds.toArray(new Long [0]), last_time, currtime, 1, 0, 1000);
					rows = object.getJSONArray("rows");//
					rows = object.getJSONArray("rows");//
					for (int k = 0; k < rows.size(); k++) {
						//rows_ = rows.getJSONObject(j).getJSONArray("c");//放一行数据
						data = new DeviceData();
						data.setEventTime(rows.getJSONObject(k).getLongValue("t"));
						data.setData(rows.getJSONObject(k).getJSONArray("c").toString());
						data.setDeviceId(box.getId());
						data.setChannelId(array.get(j).getUid());
						deviceService.addDeviceData(data);
						//json = new JSONObject();
						//json.put("time", rows.getJSONObject(k).getLongValue("t"));
						//json.put("column", rows.getJSONObject(k).getJSONArray("c"));

					}
				}

			}
			RedisPoolsUtil.set("last_time", currtime);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}  